package User;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import Log.LogManager;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jdt.internal.compiler.ast.IntLiteral;

public class UserLogin extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public UserLogin() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		try {
			response.setContentType("text/html; charset=utf-8");
			PrintWriter out = response.getWriter();
			ServletContext application=getServletContext();
			Connection con=(Connection) application.getAttribute("con");
			String username=request.getParameter("username");
			String password=request.getParameter("password");
			String sqls1="SELECT PASSWORD FROM user WHERE username='"+username+"'";
			Statement sm=con.createStatement();
			ResultSet rs=sm.executeQuery(sqls1);
			rs.last();
			int len=rs.getRow();
			rs.beforeFirst();
			if(len==0){
				out.print("username");
				out.flush();
				out.close();
				return ;
			}
			rs.next();
			if(password.equals(rs.getString(1))==false){
				out.print("password");
				out.flush();
				out.close();
				return ;
			}
			out.print("succes");
			out.flush();
			out.close();
		} catch (Exception e) {
			LogManager.err("发生错误，位置：用户登陆ajax后台，信息："+e.toString());
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
